#!/bin/bash
output="./x_build"
commandName="trojan-manager"
entryFile="main.go" # 多个文件需要用空格分开

# 启动开发服务
dev() {
    go run main.go
}

# 编译项目
buildLinux() {
    cpu=`arch`  # 获取cpu架构

    if [ $cpu = "x86_64" ];then
      CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -ldflags '-w -s' -o ${output}/linux/${commandName} ${entryFile}
    elif [ $cpu = "aarch64" ];then
      CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags '-w -s' -o ${output}/linux/${commandName} ${entryFile}
    elif [ $cpu = "armv7l" ] || [ $cpu = "armv6l" ];then
      CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags '-w -s' -o ${output}/linux/${commandName} ${entryFile}
    else
      echo "cpu架构:${cpu},未找到对应的golang编译命令"
      exit 0
    fi

    # 执行压缩
    echo "编译完成!"
}

buildWindows() {
    #交叉编译,需要将CGO_ENABLED禁止,要开启cgo需要到对应的平台编译,交叉编译是指例如在linux上编译mac可执行的文件
    CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags '-w -s' -o ${output}/windows/${commandName}.exe ${entryFile}

    # 执行压缩
#    doCompress windows
}

buildMacAmd64() {
    #交叉编译,需要将CGO_ENABLED禁止,要开启cgo需要到对应的平台编译,交叉编译是指例如在linux上编译mac可执行的文件
    CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '-w -s' -o ${output}/mac_amd64/${commandName} ${entryFile}

    # 执行压缩
#    doCompress mac
}

buildMacArm64() {
    #交叉编译,需要将CGO_ENABLED禁止,要开启cgo需要到对应的平台编译,交叉编译是指例如在linux上编译mac可执行的文件
    CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags '-w -s' -o ${output}/mac_arm64/${commandName} ${entryFile}

    # 执行压缩
#    doCompress mac
}


case "$1" in
    dev)
        $1
        exit 0
        ;;
    buildLinux)
        $1 $2
        exit 0
        ;;
    buildWindows)
        $1
        exit 0
        ;;
    buildMacAmd64)
        $1
        exit 0
        ;;
    buildMacArm64)
        $1
        exit 0
        ;;
    *)
        echo $DIR
        echo 'Usage: {dev|buildLinux|buildWindow|buildMacAmd64|buildMacArm64}'
        ;;
esac
